

<!DOCTYPE html>
<html lang="zh-CN" data-default-color-scheme=auto>



<head>
  <meta charset="UTF-8">
  <link rel="apple-touch-icon" sizes="76x76" href="https://ikkiss.oss-cn-hangzhou.aliyuncs.com/images/myblog/IKKIS_LOGO.png">
  <link rel="icon" href="https://ikkiss.oss-cn-hangzhou.aliyuncs.com/images/myblog/IKKIS_LOGO.png">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=5.0, shrink-to-fit=no">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  
  <meta name="theme-color" content="#2f4154">
  <meta name="author" content="Ikki">
  <meta name="keywords" content="">
  
    <meta name="description" content="docker-compose.yml文件详解Compose允许用户通过一个docker-compose.yml模板文件（YAML 格式）来定义一组相关联的应用容器为一个项目（project）。Compose模板文件是一个定义服务、网络和卷的YAML文件。Compose模板文件默认路径是当前目录下的docker-compose.yml，可以使用.yml或.yaml作为文件扩展名。Docker-Com">
<meta property="og:type" content="article">
<meta property="og:title" content="02.docker-compose.yml文件常用参数">
<meta property="og:url" content="http://81.68.132.127/2022/10/30/dev_ops/docker/02.docker-compose.yml%E6%96%87%E4%BB%B6%E5%B8%B8%E7%94%A8%E5%8F%82%E6%95%B0/index.html">
<meta property="og:site_name" content="IKKI">
<meta property="og:description" content="docker-compose.yml文件详解Compose允许用户通过一个docker-compose.yml模板文件（YAML 格式）来定义一组相关联的应用容器为一个项目（project）。Compose模板文件是一个定义服务、网络和卷的YAML文件。Compose模板文件默认路径是当前目录下的docker-compose.yml，可以使用.yml或.yaml作为文件扩展名。Docker-Com">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://ikkiss.oss-cn-hangzhou.aliyuncs.com/images/myblog/beijing/iTab-k75977.jpeg">
<meta property="article:published_time" content="2022-10-29T18:26:05.000Z">
<meta property="article:modified_time" content="2022-10-29T18:36:01.703Z">
<meta property="article:author" content="Ikki">
<meta property="article:tag" content="运维">
<meta property="article:tag" content="docker">
<meta name="twitter:card" content="summary_large_image">
<meta name="twitter:image" content="https://ikkiss.oss-cn-hangzhou.aliyuncs.com/images/myblog/beijing/iTab-k75977.jpeg">
  
  
    <meta name="referrer" content="no-referrer-when-downgrade">
  
  
  <title>02.docker-compose.yml文件常用参数 - IKKI</title>

  <link  rel="stylesheet" href="https://lib.baomitu.com/twitter-bootstrap/4.6.1/css/bootstrap.min.css" />



  <link  rel="stylesheet" href="https://lib.baomitu.com/github-markdown-css/4.0.0/github-markdown.min.css" />

  <link  rel="stylesheet" href="https://lib.baomitu.com/hint.css/2.7.0/hint.min.css" />

  <link  rel="stylesheet" href="https://lib.baomitu.com/fancybox/3.5.7/jquery.fancybox.min.css" />



<!-- 主题依赖的图标库，不要自行修改 -->
<!-- Do not modify the link that theme dependent icons -->

<link rel="stylesheet" href="//at.alicdn.com/t/font_1749284_hj8rtnfg7um.css">



<link rel="stylesheet" href="//at.alicdn.com/t/font_1736178_lbnruvf0jn.css">


<link  rel="stylesheet" href="/css/main.css" />


  <link id="highlight-css" rel="stylesheet" href="/css/highlight.css" />
  
    <link id="highlight-css-dark" rel="stylesheet" href="/css/highlight-dark.css" />
  



  
<link rel="stylesheet" href="/fluid/css/fluid-extention.css">
<link rel="stylesheet" href="/fluid/css/nihao.css">



  <script id="fluid-configs">
    var Fluid = window.Fluid || {};
    Fluid.ctx = Object.assign({}, Fluid.ctx)
    var CONFIG = {"hostname":"81.68.132.127","root":"/","version":"1.9.3","typing":{"enable":true,"typeSpeed":70,"cursorChar":"_","loop":false,"scope":[]},"anchorjs":{"enable":true,"element":"h1,h2,h3,h4,h5,h6","placement":"left","visible":"hover","icon":""},"progressbar":{"enable":true,"height_px":3,"color":"#29d","options":{"showSpinner":false,"trickleSpeed":100}},"code_language":{"enable":true,"default":"TEXT"},"copy_btn":true,"image_caption":{"enable":true},"image_zoom":{"enable":true,"img_url_replace":["",""]},"toc":{"enable":true,"placement":"left","headingSelector":"h1,h2,h3,h4,h5,h6","collapseDepth":3},"lazyload":{"enable":true,"loading_img":"https://ikkiss.oss-cn-hangzhou.aliyuncs.com/images/myblog/loading.gif","onlypost":false,"offset_factor":2},"web_analytics":{"enable":false,"follow_dnt":true,"baidu":null,"google":null,"gtag":null,"tencent":{"sid":null,"cid":null},"woyaola":null,"cnzz":null,"leancloud":{"app_id":null,"app_key":null,"server_url":null,"path":"window.location.pathname","ignore_local":false}},"search_path":"/local-search.xml"};

    if (CONFIG.web_analytics.follow_dnt) {
      var dntVal = navigator.doNotTrack || window.doNotTrack || navigator.msDoNotTrack;
      Fluid.ctx.dnt = dntVal && (dntVal.startsWith('1') || dntVal.startsWith('yes') || dntVal.startsWith('on'));
    }
  </script>
  <script  src="/js/utils.js" ></script>
  <script  src="/js/color-schema.js" ></script>
  


  
<meta name="generator" content="Hexo 5.4.2"></head>


<body>
  

  <header>
    

<div class="header-inner" style="height: 100vh;">
  <nav id="navbar" class="navbar fixed-top  navbar-expand-lg navbar-dark scrolling-navbar">
  <div class="container">
    <a class="navbar-brand" href="/">
      <strong>Ikki-BUG制造者</strong>
    </a>

    <button id="navbar-toggler-btn" class="navbar-toggler" type="button" data-toggle="collapse"
            data-target="#navbarSupportedContent"
            aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <div class="animated-icon"><span></span><span></span><span></span></div>
    </button>

    <!-- Collapsible content -->
    <div class="collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav ml-auto text-center">
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/">
                <i class="iconfont icon-home-fill"></i>
                首页
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/archives/">
                <i class="iconfont icon-archive-fill"></i>
                归档
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/categories/">
                <i class="iconfont icon-category-fill"></i>
                分类
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/tags/">
                <i class="iconfont icon-tags-fill"></i>
                标签
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/about/">
                <i class="iconfont icon-user-fill"></i>
                关于
              </a>
            </li>
          
        
        
          <li class="nav-item" id="search-btn">
            <a class="nav-link" target="_self" href="javascript:;" data-toggle="modal" data-target="#modalSearch" aria-label="Search">
              &nbsp;<i class="iconfont icon-search"></i>&nbsp;
            </a>
          </li>
          
        
        
          <li class="nav-item" id="color-toggle-btn">
            <a class="nav-link" target="_self" href="javascript:;" aria-label="Color Toggle">&nbsp;<i
                class="iconfont icon-dark" id="color-toggle-icon"></i>&nbsp;</a>
          </li>
        
      </ul>
    </div>
  </div>
</nav>

  

<div id="banner" class="banner" parallax=true
     style="background: url('http://image.ikki0603.cn') no-repeat center center; background-size: cover;">
  <div class="full-bg-img">
    <div class="mask flex-center" style="background-color: rgba(0, 0, 0, 0.3)">
      <div class="banner-text text-center fade-in-up">
        <div class="h2">
          
            <span id="subtitle" data-typed-text="02.docker-compose.yml文件常用参数"></span>
          
        </div>

        
          
  <div class="mt-3">
    
    
      <span class="post-meta">
        <i class="iconfont icon-date-fill" aria-hidden="true"></i>
        <time datetime="2022-10-30 02:26" pubdate>
          2022年10月30日 凌晨
        </time>
      </span>
    
  </div>

  <div class="mt-1">
    
      <span class="post-meta mr-2">
        <i class="iconfont icon-chart"></i>
        
          3.3k 字
        
      </span>
    

    
      <span class="post-meta mr-2">
        <i class="iconfont icon-clock-fill"></i>
        
        
        
          28 分钟
        
      </span>
    

    
    
      
        <span id="busuanzi_container_page_pv" style="display: none">
          <i class="iconfont icon-eye" aria-hidden="true"></i>
          <span id="busuanzi_value_page_pv"></span> 次
        </span>
        
      
    
  </div>


        
      </div>

      
        <div class="scroll-down-bar">
          <i class="iconfont icon-arrowdown"></i>
        </div>
      
    </div>
  </div>
</div>

</div>

  </header>

  <main>
    
      

<div class="container-fluid nopadding-x">
  <div class="row nomargin-x">
    <div class="side-col d-none d-lg-block col-lg-2">
      
  <aside class="sidebar" style="padding-left: 2rem; margin-right: -1rem">
    <div id="toc">
  <p class="toc-header"><i class="iconfont icon-list"></i>&nbsp;目录</p>
  <div class="toc-body" id="toc-body"></div>
</div>



  </aside>


    </div>

    <div class="col-lg-8 nopadding-x-md">
      <div class="container nopadding-x-md" id="board-ctn">
        <div id="board">
          <article class="post-content mx-auto">
            <!-- SEO header -->
            <h1 style="display: none">02.docker-compose.yml文件常用参数</h1>
            
              <p class="note note-info">
                
                  
                    本文最后更新于：2022年10月30日 凌晨
                  
                
              </p>
            
            
              <div class="markdown-body">
                
                <h6 id="docker-compose-yml文件详解"><a href="#docker-compose-yml文件详解" class="headerlink" title="docker-compose.yml文件详解"></a>docker-compose.yml文件详解</h6><p>Compose允许用户通过一个docker-compose.yml模板文件（YAML 格式）来定义一组相关联的应用容器为一个项目（project）。<br>Compose模板文件是一个定义服务、网络和卷的YAML文件。Compose模板文件默认路径是当前目录下的docker-compose.yml，可以使用.yml或.yaml作为文件扩展名。<br>Docker-Compose标准模板文件应该包含``version、services、networks` 三大部分，最关键的是services和networks两个部分。</p>
<blockquote>
<p>version</p>
</blockquote>
<p>Compose目前有三个版本分别为Version 1，Version 2，Version 3，Compose区分Version 1和Version 2（Compose 1.6.0+，Docker Engine 1.10.0+）。Version 2支持更多的指令。Version 1将来会被弃用</p>
<blockquote>
<p>services</p>
</blockquote>
<p><strong>image</strong>:是指定服务的镜像名称或镜像ID。如果镜像在本地不存在，Compose将会尝试拉取镜像。</p>
<figure class="highlight dts"><i class="iconfont icon-top" type="button" data-toggle="collapse" data-target="#collapse-hx9kevlacl5vpi"></i><span>dts</span><div class="collapse show" id="collapse-hx9kevlacl5vpi"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><code class="hljs dts"><span class="hljs-symbol">services:</span><br><span class="hljs-symbol">  web1:</span>					<span class="hljs-meta">#服务名</span><br><span class="hljs-symbol">  	image:</span> nginx<br><br></code></pre></td></tr></table></div></figure>

<p><strong>build</strong>：可以指定Dockerfile所在文件夹的路径。Compose将会利用Dockerfile自动构建镜像，然后使用镜像启动服务容器。</p>
<figure class="highlight dts"><i class="iconfont icon-top" type="button" data-toggle="collapse" data-target="#collapse-lju8u2lacl5vpi"></i><span>dts</span><div class="collapse show" id="collapse-lju8u2lacl5vpi"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><code class="hljs dts"><span class="hljs-meta">#写法一</span><br><span class="hljs-symbol">build:</span> path/Dockerfile	<br><br><span class="hljs-meta">#写法二</span><br><span class="hljs-symbol">build:</span><br><span class="hljs-symbol">  context:</span> path<br><span class="hljs-symbol">  dockerfile:</span> Dockerfile<br><br><br></code></pre></td></tr></table></div></figure>

<p>build都是一个目录，如果要指定Dockerfile文件需要在build标签的子级标签中使用dockerfile标签指定。如果同时指定image和build两个标签，那么Compose会构建镜像并且把镜像命名为image值指定的名字。</p>
<p>context选项可以是Dockerfile的文件路径，也可以是到链接到git仓库的url，当提供的值是相对路径时，被解析为相对于撰写文件的路径，此目录也是发送到Docker守护进程的context</p>
<p><strong>commond</strong>：使用command可以覆盖容器启动后默认执行的命令。</p>
<p><strong>container_name</strong>：Compose的默认容器名称格式是：&lt;项目名称&gt;&lt;服务名称&gt;&lt;序号&gt;，例如<code>webapp_web3_1</code></p>
<p>指定容器名称</p>
<figure class="highlight avrasm"><i class="iconfont icon-top" type="button" data-toggle="collapse" data-target="#collapse-bgul7ulacl5vpi"></i><span>avrasm</span><div class="collapse show" id="collapse-bgul7ulacl5vpi"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><code class="hljs avrasm"><span class="hljs-symbol">container_name:</span> web3<br><br></code></pre></td></tr></table></div></figure>

<p><strong>depends_on</strong>: 容器的启动会因容器依赖问题而启动失败，所以项目容器启动的顺序是有要求的。这个标签可以解决==容器的依赖、启动先后的问题。</p>
<figure class="highlight nestedtext"><i class="iconfont icon-top" type="button" data-toggle="collapse" data-target="#collapse-940vzmlacl5vpi"></i><span>nestedtext</span><div class="collapse show" id="collapse-940vzmlacl5vpi"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><code class="hljs nestedtext"><span class="hljs-attribute">version</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&#x27;2&#x27;</span><br><span class="hljs-attribute">services</span><span class="hljs-punctuation">:</span><br>  <span class="hljs-attribute">web</span><span class="hljs-punctuation">:</span><br>    <span class="hljs-attribute">build</span><span class="hljs-punctuation">:</span> <span class="hljs-string">.</span><br>    <span class="hljs-attribute">depends_on</span><span class="hljs-punctuation">:</span><br>      <span class="hljs-bullet">-</span> <span class="hljs-string">db</span><br>      <span class="hljs-bullet">-</span> <span class="hljs-string">redis</span><br>  <span class="hljs-attribute">redis</span><span class="hljs-punctuation">:</span><br>    <span class="hljs-attribute">image</span><span class="hljs-punctuation">:</span> <span class="hljs-string">redis</span><br>  <span class="hljs-attribute">db</span><span class="hljs-punctuation">:</span><br>    <span class="hljs-attribute">image</span><span class="hljs-punctuation">:</span> <span class="hljs-string">mysql</span><br><br></code></pre></td></tr></table></div></figure>

<p>上面的yml文件定义的容器会先启动redis和db两个服务，最后才启动web服务。</p>
<p><strong>pid</strong>：将PID模式设置为主机PID模式，跟主机系统共享进程命名空间。容器使用pid标签将能够访问和操纵其他容器和宿主机的名称空间。</p>
<figure class="highlight avrasm"><i class="iconfont icon-top" type="button" data-toggle="collapse" data-target="#collapse-lvveyplacl5vpi"></i><span>avrasm</span><div class="collapse show" id="collapse-lvveyplacl5vpi"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><code class="hljs avrasm"><span class="hljs-symbol">pid:</span> <span class="hljs-string">&quot;host&quot;</span><br><br></code></pre></td></tr></table></div></figure>

<p><strong>ports</strong>:用于映射端口的标签。使用HOST:CONTAINER格式或者只是指定容器的端口，宿主机会随机映射端口。当使用HOST:CONTAINER格式来映射端口时，如果使用的容器端口小于60可能会得到错误得结果，因为YAML将会解析xx:yy这种数字格式为60进制。所以建议采用字符串格式。</p>
<figure class="highlight avrasm"><i class="iconfont icon-top" type="button" data-toggle="collapse" data-target="#collapse-ysqr7dlacl5vpi"></i><span>avrasm</span><div class="collapse show" id="collapse-ysqr7dlacl5vpi"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><code class="hljs avrasm"><span class="hljs-symbol">ports:</span><br>  - <span class="hljs-string">&quot;3000&quot;</span><br>  - <span class="hljs-string">&quot;8000:8000&quot;</span><br>  - <span class="hljs-string">&quot;4000:22&quot;</span><br>  - <span class="hljs-string">&quot;127.0.0.1:8001:8001&quot;</span><br><br></code></pre></td></tr></table></div></figure>

<p><strong>expose</strong>:暴露端口，但不映射到宿主机，只被连接的服务访问。仅可以指定内部端口为参数</p>
<figure class="highlight avrasm"><i class="iconfont icon-top" type="button" data-toggle="collapse" data-target="#collapse-7087rflacl5vpi"></i><span>avrasm</span><div class="collapse show" id="collapse-7087rflacl5vpi"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs avrasm"><span class="hljs-symbol">expose:</span><br>  - <span class="hljs-string">&quot;3000&quot;</span><br><br></code></pre></td></tr></table></div></figure>

<p><strong>extra_hosts</strong>:添加主机名的标签，会在/etc/hosts文件中添加一些记录</p>
<figure class="highlight avrasm"><i class="iconfont icon-top" type="button" data-toggle="collapse" data-target="#collapse-ww6ke1lacl5vpi"></i><span>avrasm</span><div class="collapse show" id="collapse-ww6ke1lacl5vpi"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><code class="hljs avrasm"><span class="hljs-symbol">extra_hosts:</span><br>  - <span class="hljs-string">&quot;hostname1:IP&quot;</span><br>  - <span class="hljs-string">&quot;hostname2:IP&quot;</span><br><br></code></pre></td></tr></table></div></figure>

<p>启动后进入容器查看==/etc/hosts/==文件</p>
<figure class="highlight gcode"><i class="iconfont icon-top" type="button" data-toggle="collapse" data-target="#collapse-3257yolacl5vpi"></i><span>gcode</span><div class="collapse show" id="collapse-3257yolacl5vpi"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs gcode">IP	host<span class="hljs-symbol">name1</span><br>IP	host<span class="hljs-symbol">name2</span><br><br></code></pre></td></tr></table></div></figure>

<p><strong>volumes</strong>:挂载一个目录或者一个已存在的数据卷容器，可以直接使用 [HOST:CONTAINER]格式，或者使用[HOST:CONTAINER:ro]格式，后者对于容器来说，数据卷是只读的，可以有效保护宿主机的文件系统。Compose的数据卷指定路径可以是相对路径，使用 <code>.</code> 或者 <code>..</code> 来指定相对目录。</p>
<figure class="highlight gradle"><i class="iconfont icon-top" type="button" data-toggle="collapse" data-target="#collapse-2rrrtqlacl5vpi"></i><span>gradle</span><div class="collapse show" id="collapse-2rrrtqlacl5vpi"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><code class="hljs gradle">volumes:<br>  # 只是指定一个路径，Docker 会自动在创建一个数据卷（这个路径是容器内部的）。<br>  - <span class="hljs-regexp">/var/</span>lib/mysql<br><br>  # 使用绝对路径挂载数据卷<br>  - <span class="hljs-regexp">/opt/</span>data:<span class="hljs-regexp">/var/</span>lib/mysql<br><br>  # 以 Compose 配置文件为中心的相对路径作为数据卷挂载到容器。<br>  - .<span class="hljs-regexp">/cache:/</span>tmp/cache<br><br>  # 使用用户的相对路径（~<span class="hljs-regexp">/ 表示的目录是 /</span>home<span class="hljs-regexp">/&lt;用户目录&gt;/</span> 或者 <span class="hljs-regexp">/root/</span>）。<br>  - ~<span class="hljs-regexp">/configs:/</span>etc<span class="hljs-regexp">/configs/</span>:ro<br><br>  # 已经存在的命名的数据卷。<br>  - datavolume:<span class="hljs-regexp">/var/</span>lib/mysql<br><br></code></pre></td></tr></table></div></figure>

<p>如果你不使用宿主机的路径，可以指定一个 <code>volume_driver</code></p>
<figure class="highlight avrasm"><i class="iconfont icon-top" type="button" data-toggle="collapse" data-target="#collapse-w4m9fslacl5vpi"></i><span>avrasm</span><div class="collapse show" id="collapse-w4m9fslacl5vpi"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><code class="hljs avrasm"><span class="hljs-symbol">volume_driver:</span> mydriver<br><br></code></pre></td></tr></table></div></figure>

<p><strong>volumes_from</strong>:从其它容器或者服务挂载数据卷，可选的参数是 <code>:ro</code> 或 <code>:rw</code>，前者表示容器只读，后者表示容器对数据卷是可读可写的（默认情况为可读可写的）。</p>
<figure class="highlight nestedtext"><i class="iconfont icon-top" type="button" data-toggle="collapse" data-target="#collapse-40j590lacl5vpi"></i><span>nestedtext</span><div class="collapse show" id="collapse-40j590lacl5vpi"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><code class="hljs nestedtext"><span class="hljs-attribute">volumes_from</span><span class="hljs-punctuation">:</span><br>  <span class="hljs-bullet">-</span> <span class="hljs-string">service_name</span><br>  <span class="hljs-bullet">-</span> <span class="hljs-string">service_name:ro</span><br>  <span class="hljs-bullet">-</span> <span class="hljs-string">container:container_name</span><br>  <span class="hljs-bullet">-</span> <span class="hljs-string">container:container_name:rw</span><br><br></code></pre></td></tr></table></div></figure>

<p><strong>links</strong>：链接到其它服务中的容器。使用服务名称（同时作为别名）或服务名称：服务别名 （SERVICE:ALIAS） 格式都可以</p>
<figure class="highlight nestedtext"><i class="iconfont icon-top" type="button" data-toggle="collapse" data-target="#collapse-4tctyylacl5vpi"></i><span>nestedtext</span><div class="collapse show" id="collapse-4tctyylacl5vpi"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><code class="hljs nestedtext"><span class="hljs-attribute">links</span><span class="hljs-punctuation">:</span><br>    <span class="hljs-bullet">-</span> <span class="hljs-string">db</span><br>    <span class="hljs-bullet">-</span> <span class="hljs-string">db:database</span><br>    <span class="hljs-bullet">-</span> <span class="hljs-string">redis</span><br><br></code></pre></td></tr></table></div></figure>

<p>使用的别名将会自动在服务容器中的 /etc/hosts 里创建。相应的环境变量也将被创建。例如：</p>
<figure class="highlight x86asm"><i class="iconfont icon-top" type="button" data-toggle="collapse" data-target="#collapse-9mx576lacl5vpi"></i><span>x86asm</span><div class="collapse show" id="collapse-9mx576lacl5vpi"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><code class="hljs x86asm">容器<span class="hljs-built_in">IP</span> <span class="hljs-built_in">db</span><br><br></code></pre></td></tr></table></div></figure>

<p><strong>external_links</strong>：链接到 docker-compose.yml 外部的容器，甚至 并非 Compose 管理的容器。参数格式跟 links 类似。在使用Docker过程中，会有许多单独使用 docker run 启动的容器的情况，为了使 Compose 能够连接这些不在docker-compose.yml 配置文件中定义的容器，那么就需要一个特殊的标签，就是 external_links，它可以让Compose 项目里面的容器连接到那些项目配置外部的容器（前提是外部容器中必须至少有一个容器是连接到与项目内的服务的同一个网络里面）</p>
<figure class="highlight nestedtext"><i class="iconfont icon-top" type="button" data-toggle="collapse" data-target="#collapse-6ggvgqlacl5vpi"></i><span>nestedtext</span><div class="collapse show" id="collapse-6ggvgqlacl5vpi"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><code class="hljs nestedtext"><span class="hljs-attribute">external_links</span><span class="hljs-punctuation">:</span><br>	<span class="hljs-bullet">-</span> <span class="hljs-string">redis_1</span><br>	<span class="hljs-bullet">-</span> <span class="hljs-string">project_db_1:mysql</span><br>	<span class="hljs-bullet">-</span> <span class="hljs-string">project_db_1:postgresql</span><br><br></code></pre></td></tr></table></div></figure>

<p><strong>net</strong>:设置网络模式。</p>
<figure class="highlight dts"><i class="iconfont icon-top" type="button" data-toggle="collapse" data-target="#collapse-2qz262lacl5vpi"></i><span>dts</span><div class="collapse show" id="collapse-2qz262lacl5vpi"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><code class="hljs dts"><span class="hljs-symbol">net:</span> <span class="hljs-string">&quot;bridge&quot;</span>	<span class="hljs-meta">#<span class="hljs-string">&quot;none&quot;</span>	<span class="hljs-string">&quot;host&quot;</span></span><br><br></code></pre></td></tr></table></div></figure>

<p><strong>dns</strong>:自定义DNS服务器。可以是一个值，也可以是一个列表。</p>
<figure class="highlight dns"><i class="iconfont icon-top" type="button" data-toggle="collapse" data-target="#collapse-63250slacl5vpi"></i><span>dns</span><div class="collapse show" id="collapse-63250slacl5vpi"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><code class="hljs dns">dns：<span class="hljs-number">8.8.8.8</span><br>dns：<br>    - <span class="hljs-number">8.8.8.8</span>   <br>    - <span class="hljs-number">9.9.9.9</span><br><br></code></pre></td></tr></table></div></figure>

<p>本文转自 <a target="_blank" rel="noopener" href="https://blog.csdn.net/weixin_44334234/article/details/106240551">https://blog.csdn.net/weixin_44334234/article/details/106240551</a>，如有侵权，请联系删除。</p>

                
              </div>
            
            <hr/>
            <div>
              <div class="post-metas my-3">
  
    <div class="post-meta mr-3 d-flex align-items-center">
      <i class="iconfont icon-category"></i>
      

<span class="category-chains">
  
  
    
      <span class="category-chain">
        
  <a href="/categories/%E8%BF%90%E7%BB%B4/" class="category-chain-item">运维</a>
  
  
    <span>></span>
    
  <a href="/categories/%E8%BF%90%E7%BB%B4/docker/" class="category-chain-item">docker</a>
  
  

  

      </span>
    
  
</span>

    </div>
  
  
    <div class="post-meta">
      <i class="iconfont icon-tags"></i>
      
        <a href="/tags/%E8%BF%90%E7%BB%B4/">#运维</a>
      
        <a href="/tags/docker/">#docker</a>
      
    </div>
  
</div>


              
  

  <div class="license-box my-3">
    <div class="license-title">
      <div>02.docker-compose.yml文件常用参数</div>
      <div>http://81.68.132.127/2022/10/30/dev_ops/docker/02.docker-compose.yml文件常用参数/</div>
    </div>
    <div class="license-meta">
      
        <div class="license-meta-item">
          <div>作者</div>
          <div>Ikki</div>
        </div>
      
      
        <div class="license-meta-item license-meta-date">
          <div>发布于</div>
          <div>2022年10月30日</div>
        </div>
      
      
        <div class="license-meta-item license-meta-date">
          <div>更新于</div>
          <div>2022年10月30日</div>
        </div>
      
      
        <div class="license-meta-item">
          <div>许可协议</div>
          <div>
            
              
              
                <a target="_blank" href="https://creativecommons.org/licenses/by/4.0/">
                  <span class="hint--top hint--rounded" aria-label="BY - 署名">
                    <i class="iconfont icon-by"></i>
                  </span>
                </a>
              
            
          </div>
        </div>
      
    </div>
    <div class="license-icon iconfont"></div>
  </div>



              
                <div class="post-prevnext my-3">
                  <article class="post-prev col-6">
                    
                    
                      <a href="/2022/10/31/react/router/%E5%AE%9E%E7%8E%B0%E7%AE%80%E7%89%88router/" title="实现一个简版mini-router">
                        <i class="iconfont icon-arrowleft"></i>
                        <span class="hidden-mobile">实现一个简版mini-router</span>
                        <span class="visible-mobile">上一篇</span>
                      </a>
                    
                  </article>
                  <article class="post-next col-6">
                    
                    
                      <a href="/2022/10/27/dev_ops/linux/10.%E7%BD%91%E7%BB%9C/" title="10.linux网络">
                        <span class="hidden-mobile">10.linux网络</span>
                        <span class="visible-mobile">下一篇</span>
                        <i class="iconfont icon-arrowright"></i>
                      </a>
                    
                  </article>
                </div>
              
            </div>

            
  <article id="comments" lazyload>
    
  <div id="valine"></div>
  <script type="text/javascript">
    Fluid.utils.loadComments('#valine', function() {
      Fluid.utils.createScript('https://lib.baomitu.com/valine/1.5.1/Valine.min.js', function() {
        var options = Object.assign(
          {"appId":"6KzYWc2oaEBaHgNl4UIicMJC-gzGzoHsz","appKey":"vJ4FIwKeHRmHraCJraAsjyZ7","path":"window.location.pathname","placeholder":"请输入你的评论\n昵称为必填项目(输入QQ号码可以自动获取昵称和邮箱)\n虽然email不是必选，但是填写了email可以收到推送通知哦！","avatar":"retro","meta":["nick","mail","link"],"requiredFields":[],"pageSize":10,"lang":"zh-CN","highlight":true,"recordIP":true,"serverURLs":"","emojiCDN":null,"emojiMaps":null,"enableQQ":true},
          {
            el: "#valine",
            path: window.location.pathname
          }
        )
        new Valine(options);
        Fluid.utils.waitElementVisible('#valine .vcontent', () => {
          var imgSelector = '#valine .vcontent img:not(.vemoji)';
          Fluid.plugins.imageCaption(imgSelector);
          Fluid.plugins.fancyBox(imgSelector);
        })
      });
    });
  </script>
  <noscript>Please enable JavaScript to view the comments</noscript>


  </article>


          </article>
        </div>
      </div>
    </div>

    <div class="side-col d-none d-lg-block col-lg-2">
      

    </div>
  </div>
</div>





  



  



  



  



  







    

    
      <a id="scroll-top-button" aria-label="TOP" href="#" role="button">
        <i class="iconfont icon-arrowup" aria-hidden="true"></i>
      </a>
    

    
      <div class="modal fade" id="modalSearch" tabindex="-1" role="dialog" aria-labelledby="ModalLabel"
     aria-hidden="true">
  <div class="modal-dialog modal-dialog-scrollable modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header text-center">
        <h4 class="modal-title w-100 font-weight-bold">搜索</h4>
        <button type="button" id="local-search-close" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body mx-3">
        <div class="md-form mb-5">
          <input type="text" id="local-search-input" class="form-control validate">
          <label data-error="x" data-success="v" for="local-search-input">关键词</label>
        </div>
        <div class="list-group" id="local-search-result"></div>
      </div>
    </div>
  </div>
</div>

    

    
  </main>

  <footer>
    <div class="footer-inner">
  
    <div class="footer-content">
      IKKI-专业BUG开发
    </div>
  
  
    <div class="statistics">
  
  

  
    
      <span id="busuanzi_container_site_pv" style="display: none">
        总访问量 
        <span id="busuanzi_value_site_pv"></span>
         次
      </span>
    
    
      <span id="busuanzi_container_site_uv" style="display: none">
        总访客数 
        <span id="busuanzi_value_site_uv"></span>
         人
      </span>
    
    
  
</div>

  
  
  
</div>

  </footer>

  <!-- Scripts -->
  
  <script  src="https://lib.baomitu.com/nprogress/0.2.0/nprogress.min.js" ></script>
  <link  rel="stylesheet" href="https://lib.baomitu.com/nprogress/0.2.0/nprogress.min.css" />

  <script>
    NProgress.configure({"showSpinner":false,"trickleSpeed":100})
    NProgress.start()
    window.addEventListener('load', function() {
      NProgress.done();
    })
  </script>


<script  src="https://lib.baomitu.com/jquery/3.6.0/jquery.min.js" ></script>
<script  src="https://lib.baomitu.com/twitter-bootstrap/4.6.1/js/bootstrap.min.js" ></script>
<script  src="/js/events.js" ></script>
<script  src="/js/plugins.js" ></script>


  <script  src="https://lib.baomitu.com/typed.js/2.0.12/typed.min.js" ></script>
  <script>
    (function (window, document) {
      var typing = Fluid.plugins.typing;
      var subtitle = document.getElementById('subtitle');
      if (!subtitle || !typing) {
        return;
      }
      var text = subtitle.getAttribute('data-typed-text');
      
        typing(text);
      
    })(window, document);
  </script>




  
    <script  src="/js/img-lazyload.js" ></script>
  




  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/tocbot/4.18.2/tocbot.min.js', function() {
    var toc = jQuery('#toc');
    if (toc.length === 0 || !window.tocbot) { return; }
    var boardCtn = jQuery('#board-ctn');
    var boardTop = boardCtn.offset().top;

    window.tocbot.init(Object.assign({
      tocSelector     : '#toc-body',
      contentSelector : '.markdown-body',
      linkClass       : 'tocbot-link',
      activeLinkClass : 'tocbot-active-link',
      listClass       : 'tocbot-list',
      isCollapsedClass: 'tocbot-is-collapsed',
      collapsibleClass: 'tocbot-is-collapsible',
      scrollSmooth    : true,
      includeTitleTags: true,
      headingsOffset  : -boardTop,
    }, CONFIG.toc));
    if (toc.find('.toc-list-item').length > 0) {
      toc.css('visibility', 'visible');
    }

    Fluid.events.registerRefreshCallback(function() {
      if ('tocbot' in window) {
        tocbot.refresh();
        var toc = jQuery('#toc');
        if (toc.length === 0 || !tocbot) {
          return;
        }
        if (toc.find('.toc-list-item').length > 0) {
          toc.css('visibility', 'visible');
        }
      }
    });
  });
</script>


  <script src=https://lib.baomitu.com/clipboard.js/2.0.11/clipboard.min.js></script>

  <script>Fluid.plugins.codeWidget();</script>


  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/anchor-js/4.3.1/anchor.min.js', function() {
    window.anchors.options = {
      placement: CONFIG.anchorjs.placement,
      visible  : CONFIG.anchorjs.visible
    };
    if (CONFIG.anchorjs.icon) {
      window.anchors.options.icon = CONFIG.anchorjs.icon;
    }
    var el = (CONFIG.anchorjs.element || 'h1,h2,h3,h4,h5,h6').split(',');
    var res = [];
    for (var item of el) {
      res.push('.markdown-body > ' + item.trim());
    }
    if (CONFIG.anchorjs.placement === 'left') {
      window.anchors.options.class = 'anchorjs-link-left';
    }
    window.anchors.add(res.join(', '));

    Fluid.events.registerRefreshCallback(function() {
      if ('anchors' in window) {
        anchors.removeAll();
        var el = (CONFIG.anchorjs.element || 'h1,h2,h3,h4,h5,h6').split(',');
        var res = [];
        for (var item of el) {
          res.push('.markdown-body > ' + item.trim());
        }
        if (CONFIG.anchorjs.placement === 'left') {
          anchors.options.class = 'anchorjs-link-left';
        }
        anchors.add(res.join(', '));
      }
    });
  });
</script>


  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/fancybox/3.5.7/jquery.fancybox.min.js', function() {
    Fluid.plugins.fancyBox();
  });
</script>


  <script>Fluid.plugins.imageCaption();</script>

  <script  src="/js/local-search.js" ></script>

  <script defer src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js" ></script>




  
<script src="/fluid/js/star.js"></script>



<!-- 主题的启动项，将它保持在最底部 -->
<!-- the boot of the theme, keep it at the bottom -->
<script  src="/js/boot.js" ></script>


  

  <noscript>
    <div class="noscript-warning">博客在允许 JavaScript 运行的环境下浏览效果更佳</div>
  </noscript>
</body>
</html>
